**************************************************************
***** 		Local Projections 				******
**************************************************************

/*
This replication code generates the local projections  in the
paper "Populism and De Facto Central Bank Independence"
(Gavin and Manger). All code is for Stata version 14.2
unless indicated. It also exports the data required to produce the graphs
in R.

We use Jordà (2005) local projections to estimate the impulse reponse of inflation and
the monetary Rate to a state-dependent event of public pressure on the CB

*/

capture log close

		clear all

		macro drop _all
		graph drop _all

* set this to your path
global folder "~/path_to_replication_package"

		use "$folder/D01_DeFactoCBI.dta", clear


gen countrySample = 0

			replace countrySample = 1 if iso =="ALB" | iso == "ARG" |  ///
			iso == "BOL" | iso == "BWA" | iso == "CHL" | iso == "COL" | iso == "CRI" | ///
			iso == "DOM" | iso == "GEO" | iso == "GHA" | iso == "GTM" | ///
			iso == "IDN" | iso == "ISR" | iso == "KEN" | iso == "LKA" | iso == "MDA" | ///
			iso == "MEX" | iso == "MNG" | iso == "MUS" | iso == "MYS" | iso == "NGA" | ///
			iso == "NIC" | iso == "PER" | iso == "PHL" | iso == "POL" | iso == "PRY" | ///
			iso == "RUS" | iso == "THA" | ///
			iso == "TUN" | iso == "TUR" | iso == "UKR" | ///
			iso == "URY" | iso == "VEN" | iso == "ZMB"

	keep if countrySample == 1 & year >= 1996 & xrRegimeFine > 2  & xrRegimeFine < 14


	sort id quarterlydate


tsset id quarterlydate

gen Cpi = cpi

* generate state variables: low populism vs high populism regimes

* gen populiststate = populism_vparty >= 0.6505345 /* This is exactly mean + 1 sd */
gen populiststate = populism_vparty >= 0.75

gen nonpopstate = 1-populiststate

gen pressure = politicalPressure

	label var populiststate "=1 if government is populist"

gen popXshock = populiststate * pressure
gen nonPopXshock = nonpopstate * pressure

foreach variable in lnExchRate monetaryRate lnCpi Cpi netflow2gdp   {
	gen d`variable' = d.`variable'
	}


*******************
** Set-up of LPs **
*******************

***** LHS variable: the response variable
forvalues i=0/12 {

		gen dlnCpi`i' = (f`i'.dlnCpi - l.dlnCpi)
		gen dCpi`i' = (f`i'.dCpi - l.dCpi)
		gen dmonetaryRate`i' = (f`i'.dmonetaryRate - l.dmonetaryRate)
		gen dnetflow2gdp`i' = (f`i'.dnetflow2gdp - l.dnetflow2gdp)

	}

	***** variables to store the impulse response (vector of betas from the LP regressions) and standard errors
	gen b_CpiPopPressure=.
	gen b_ratePopPressure=.
	gen se_CpiPopPressure=.
	gen se_ratePopPressure=.
	gen ci_ul90_CpiPopPressure=.
	gen ci_ll90_CpiPopPressure=.
	gen ci_ul95_CpiPopPressure=.
	gen ci_ll95_CpiPopPressure=.
	gen ci_ul90_ratePopPressure=.
	gen ci_ll90_ratePopPressure=.
	gen ci_ul95_ratePopPressure=.
	gen ci_ll95_ratePopPressure=.

  ***** identical for non-populist pressure

	gen b_CpiNonPopPressure=.
	gen b_rateNonPopPressure=.
	gen se_CpiNonPopPressure=.
	gen se_rateNonPopPressure=.
	gen ci_ul90_CpiNonPopPressure=.
	gen ci_ll90_CpiNonPopPressure=.
	gen ci_ul95_CpiNonPopPressure=.
	gen ci_ll95_CpiNonPopPressure=.
	gen ci_ul90_rateNonPopPressure=.
	gen ci_ll90_rateNonPopPressure=.
	gen ci_ul95_rateNonPopPressure=.
	gen ci_ll95_rateNonPopPressure=.

* number of lags
local MaxLPLags 2


* horizon
local horizon 8

* variables for the p-value test

forvalues h = 0/`horizon' {
    gen testp_rate`h' = .
    gen testp_cpi`h' = .
	}


****************************************************************
**** Controls in LP regression for the monetary rate
****************************************************************

forvalues j=0/`MaxLPLags' {

	foreach rate_rhsvariable of varlist dnetflow2gdp dCpi dlnExchRate {

		gen l`j'`rate_rhsvariable'populiststate = L`j'.`rate_rhsvariable' * populiststate
		gen l`j'`rate_rhsvariable'nonpopstate = L`j'.`rate_rhsvariable' * nonpopstate

 	}
 }

 **** exclude the contemporaneous lag of impulse (pressure) and response (monetaryRate)

forvalues j=1/`MaxLPLags' {

 foreach impulse_response of varlist politicalPressure dmonetaryRate {

 gen l`j'`impulse_response'populiststate = L`j'.`impulse_response' * populiststate

 gen l`j'`impulse_response'nonpopstate = L`j'.`impulse_response' * nonpopstate

 	}
 }

local rhsmonetaryRatePressurePop l?dCpipopuliststate l?dlnExchRatepopuliststate l?dnetflow2gdppopuliststate l?politicalPressurepopuliststate l?dmonetaryRatepopuliststate

local rhsmonetaryRatePressureNonPop l?dCpinonpopstate l?dlnExchRatenonpopstate l?dnetflow2gdpnonpopstate l?politicalPressurenonpopstate l?dmonetaryRatenonpopstate


sort id quarterlydate

****************************************************************
***** LP regressions for the monetary rate
****************************************************************

forvalues j=0/`horizon' {

		* monetaryRate LP
quietly: xtreg dmonetaryRate`j' popXshock nonPopXshock `rhsmonetaryRatePressurePop' `rhsmonetaryRatePressureNonPop', fe cluster(id)

		  eststo ols_monetaryRatePressure`j'
		  replace b_ratePopPressure  = _b[popXshock] if _n == `j'+1
		  replace se_ratePopPressure = _se[popXshock] if _n == `j'+1
	*		repeat the same for the non-populist state
	    replace b_rateNonPopPressure  = _b[nonPopXshock] if _n == `j'+1
	    replace se_rateNonPopPressure = _se[nonPopXshock] if _n == `j'+1

			test popXshock - nonPopXshock = 0
			replace testp_rate`j' = r(p)

}

***** labels for monetaryRate path tables
forvalues k=0/`horizon' {
	label var dmonetaryRate`k' "Quarter `k'"
		}

****************************************************************
*** Baseline LP table Using OLS for the monetary rate
****************************************************************


esttab ols_monetaryRatePressure0 ols_monetaryRatePressure1 ols_monetaryRatePressure2 ols_monetaryRatePressure3 ols_monetaryRatePressure4 ///
				ols_monetaryRatePressure5 ols_monetaryRatePressure6 ols_monetaryRatePressure7 ols_monetaryRatePressure8 ///
			    using "$folder/table_ols_monetaryRate.tex", page replace title("Monetary Rate Local Projection") ///
					se r2 keep(popXshock nonPopXshock) coeflabels(popXshock "Populist pressure" nonPopXshock "Non-populist pressure")  nonum ///
					b(2) se(2) sfmt(2) obslast  label star(* 0.05 ** 0.01 *** 0.001)


****************************************************************
***  OLS LP graphs for the monetary rate
****************************************************************

***** create confidence bands (in this case 90 and 95%) ****
scalar sig1 = 0.1	 // specify significance level
scalar sig2 = 0.05	 // specify significance level
scalar sig3 = 0.01

* populist pressure on the monetary rate

 replace ci_ul90_ratePopPressure = b_ratePopPressure + invnormal(1-sig1/2)*se_ratePopPressure if _n <= (`horizon' + 1)
 replace ci_ll90_ratePopPressure = b_ratePopPressure - invnormal(1-sig1/2)*se_ratePopPressure if _n <= (`horizon' + 1)
 replace ci_ul95_ratePopPressure = b_ratePopPressure + invnormal(1-sig2/2)*se_ratePopPressure if _n <= (`horizon' + 1)
 replace ci_ll95_ratePopPressure = b_ratePopPressure - invnormal(1-sig2/2)*se_ratePopPressure if _n <= (`horizon' + 1)
* non-populist pressure on the monetary rate

 replace ci_ul90_rateNonPopPressure = b_rateNonPopPressure + invnormal(1-sig1/2)*se_rateNonPopPressure if _n <= (`horizon' + 1)
 replace ci_ll90_rateNonPopPressure = b_rateNonPopPressure - invnormal(1-sig1/2)*se_rateNonPopPressure if _n <= (`horizon' + 1)
 replace ci_ul95_rateNonPopPressure = b_rateNonPopPressure + invnormal(1-sig2/2)*se_rateNonPopPressure if _n <= (`horizon' + 1)
 replace ci_ll95_rateNonPopPressure = b_rateNonPopPressure - invnormal(1-sig2/2)*se_rateNonPopPressure if _n <= (`horizon' + 1)



gen quarterAfterShock = .
	forvalues q = 0/`horizon' {
	replace quarterAfterShock = `q' if _n == `q'+1
	 }


****************************************************************
**** Controls in LP regression for the inflation rate
**** we need to create the contemporaneous impulse and response
**** that we didn't create above but now need.
****************************************************************

forvalues j=0/0 {

	foreach cpi_rhsvariable of varlist dmonetaryRate {

	gen l`j'`cpi_rhsvariable'populiststate = L`j'.`cpi_rhsvariable' * populiststate

	gen l`j'`cpi_rhsvariable'nonpopstate = L`j'.`cpi_rhsvariable' * nonpopstate

	}

}

**** exclude the contemporaneous lag of impulse (pressure) and response (cpi)
drop l0dCpipopuliststate l0dCpinonpopstate


local rhsCpiPressurePop l?dCpipopuliststate l?dlnExchRatepopuliststate l?dnetflow2gdppopuliststate l?politicalPressurepopuliststate l?dmonetaryRatepopuliststate

local rhsCpiPressureNonPop l?dCpinonpopstate l?dlnExchRatenonpopstate l?dnetflow2gdpnonpopstate l?politicalPressurenonpopstate l?dmonetaryRatenonpopstate

****************************************************************
***** LP regression for CPI
****************************************************************

forvalues j=0/`horizon' {

	quietly: xtreg dCpi`j' popXshock nonPopXshock `rhsCpiPressurePop' `rhsCpiPressureNonPop', fe cluster(id)
	eststo ols_CpiPressure`j'


	replace b_CpiPopPressure  = _b[popXshock] if _n == `j'+1
	replace se_CpiPopPressure = _se[popXshock] if _n == `j'+1

	replace b_CpiNonPopPressure  = _b[nonPopXshock] if _n == `j'+1
	replace se_CpiNonPopPressure = _se[nonPopXshock] if _n == `j'+1
	test popXshock - nonPopXshock = 0
	replace testp_cpi`j' = r(p)

	}



  * labels for Cpi path tables
forvalues i=0/`horizon' {
  label var dCpi`i' "Quarter `i'"
  }

****************************************************************
*** Baseline LP table Using OLS for the monetary rate
****************************************************************
esttab ols_CpiPressure0 ols_CpiPressure1 ols_CpiPressure2 ols_CpiPressure3 ols_CpiPressure4 ///
	ols_CpiPressure5 ols_CpiPressure6 ols_CpiPressure7 ols_CpiPressure8 ///
		using "$folder/table_ols_Cpi.tex", page replace title("Inflation Local Projection") ///
		se r2 keep(popXshock nonPopXshock) coeflabels(popXshock "Populist pressure" nonPopXshock "Non-populist pressure")  nonum ///
		b(2) se(2) sfmt(2) obslast  label star(* 0.05 ** 0.01 *** 0.001)


****************************************************************
***  OLS LP graphs for inflation
****************************************************************

* populist pressure on inflation

replace ci_ul90_CpiPopPressure = b_CpiPopPressure + invnormal(1-sig1/2)*se_CpiPopPressure if _n <= (`horizon' + 1)
replace ci_ll90_CpiPopPressure = b_CpiPopPressure - invnormal(1-sig1/2)*se_CpiPopPressure if _n <= (`horizon' + 1)
replace ci_ul95_CpiPopPressure = b_CpiPopPressure + invnormal(1-sig2/2)*se_CpiPopPressure if _n <= (`horizon' + 1)
replace ci_ll95_CpiPopPressure = b_CpiPopPressure - invnormal(1-sig2/2)*se_CpiPopPressure if _n <= (`horizon' + 1)

  * non-populist pressure on inflation

replace ci_ul90_CpiNonPopPressure = b_CpiNonPopPressure + invnormal(1-sig1/2)*se_CpiNonPopPressure if _n <= (`horizon' + 1)
replace ci_ll90_CpiNonPopPressure = b_CpiNonPopPressure - invnormal(1-sig1/2)*se_CpiNonPopPressure if _n <= (`horizon' + 1)
replace ci_ul95_CpiNonPopPressure = b_CpiNonPopPressure + invnormal(1-sig2/2)*se_CpiNonPopPressure if _n <= (`horizon' + 1)
replace ci_ll95_CpiNonPopPressure = b_CpiNonPopPressure - invnormal(1-sig2/2)*se_CpiNonPopPressure if _n <= (`horizon' + 1)

preserve
keep quarterAfterShock b_CpiPopPressure  se_CpiPopPressure  ///
ci_ul90_CpiPopPressure ci_ll90_CpiPopPressure ci_ul95_CpiPopPressure ci_ll95_CpiPopPressure ///
b_CpiNonPopPressure  se_CpiNonPopPressure  ci_ul90_CpiNonPopPressure ///
ci_ll90_CpiNonPopPressure ci_ul95_CpiNonPopPressure ci_ll95_CpiNonPopPressure ///
b_ratePopPressure se_ratePopPressure ci_ul90_ratePopPressure ci_ll90_ratePopPressure ///
ci_ul95_ratePopPressure ci_ll95_ratePopPressure ///
b_rateNonPopPressure se_rateNonPopPressure ci_ul90_rateNonPopPressure ///
ci_ll90_rateNonPopPressure ci_ll95_rateNonPopPressure ci_ul95_rateNonPopPressure

local maxObs = `horizon' + 1
drop if _n > `maxObs'
export delimited using "$folder/D04_PressureInflation-LPgraphdata.csv", replace

restore

preserve
keep testp_rate*
format %9.3f testp_rate*
keep if _n == 1
export delimited using "$folder/Tests-P-value-Rate.csv",  delimiter("&") replace novarnames datafmt
restore

preserve
keep testp_cpi*
format %9.3f testp_cpi*
keep if _n == 1
export delimited using "$folder/Tests-P-value-CPI.csv",  delimiter("&") replace novarnames datafmt
restore

* log close
